package com.zhy.http.library.request;

import com.zhy.http.library.OkHttpUtils;
import com.zhy.http.library.callback.Callback;
import com.zhy.http.library.utils.Exceptions;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.File;
import java.util.Map;

/**
 * Created by zhy on 15/12/14.
 */
public class PostFileRequest extends OkHttpRequest {
    private static MediaType MEDIA_TYPE_STREAM = MediaType.parse("application/octet-stream");

    private File file;
    private MediaType mediaType;

    public PostFileRequest(
            String url,
            Object tag,
            Map<String, String> params,
            Map<String, String> headers,
            File file,
            MediaType mediaType,
            int id) {
        super(url, tag, params, headers, id);
        this.file = file;
        this.mediaType = mediaType;

        if (this.file == null) {
            Exceptions.illegalArgument("the file can not be null !");
        }
        if (this.mediaType == null) {
            this.mediaType = MEDIA_TYPE_STREAM;
        }
    }

    @Override
    protected RequestBody buildRequestBody() {
        return RequestBody.create(mediaType, file);
    }

    @Override
    protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) {
        if (callback == null) return requestBody;
        CountingRequestBody countingRequestBody =
                new CountingRequestBody(
                        requestBody,
                        new CountingRequestBody.Listener() {
                            @Override
                            public void onRequestProgress(final long bytesWritten, final long contentLength) {
                                OkHttpUtils.getInstance()
                                        .getDelivery()
                                        .execute(
                                                new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        callback.inProgress(
                                                                bytesWritten * 1.0f / contentLength, contentLength, id);
                                                    }
                                                });
                            }
                        });
        return countingRequestBody;
    }

    @Override
    protected Request buildRequest(RequestBody requestBody) {
        return builder.post(requestBody).build();
    }
}
